<?php

namespace Home\Controller;

use Think\Controller;

class UserGroupController extends Controller {

    public function index() {
        $this->display("UserGroup");
    }

    /**
     * 查询所有用户分组
     */
    public function GetUserGroupAll() {
        $model = M('followerusergroup');
        $condition['PublicCodeID'] = 1;
        $groupList = $model->where($condition)->select();
        $childrenarray = array();
        if (count($groupList) == 0) {
            $wechatHelper = new WechatHelperController();
            $array = $wechatHelper->get_group();
            $grouparray = $array["groups"];
            for ($i = 0; $i < count($grouparray); $i++) {
                $group = array("id" => $grouparray[$i]["id"], "text" => $grouparray[$i]["name"]);
                array_push($childrenarray, $group);
                $data["WeChatGroupID"] = $grouparray[$i]["id"];
                $data["GroupName"] = $grouparray[$i]["name"];
                $data["PublicCodeID"] = 1;
                $addresult = $model->data($data)->add();
            }
        } else {
            for ($i = 0; $i < count($groupList); $i++) {
                $group = array("id" => $groupList[$i]["WeChatGroupID"], "text" => $groupList[$i]["GroupName"]);
                array_push($childrenarray, $group);
            }
        }
        $viewarray = array("id" => "", "text" => "用户组", "children" => $childrenarray);
        $json = "[" . json_encode($viewarray, JSON_UNESCAPED_UNICODE) . "]";
        $this->ajaxReturn($json, 'EVAL');
    }

    /**
     * 获取所有关注者用户
     */
    public function GetUser() {
        $groupID = I('groupId', '', 'htmlspecialchars');
        $userName = I('userName', '', 'htmlspecialchars');
        $referUser=I('IsUpdate','','htmlspecialchars');//是否强制更新
        $followUser = M("followeruser");
        $condition['PublicCodeID'] = 1;
        if ($groupID != '') {
            $condition['WechatGroupID'] = $groupID;
        }
        if ($userName != '') {
            $condition['NickName'] = array("like", "%" . $userName . "%");
        }
        $userList = $followUser->where($condition)->select();
        if ((count($userList) == 0 && $groupID === '' && $userName === '')||$referUser==1) {
            $followUser->where($condition)->delete();//先清空所有关注者用户再重新获取
            $wechatHelper = new WechatHelperController();
            $array = $wechatHelper->get_user_list();
            $openIDList = $array["data"]["openid"];
            $total = count($openIDList);
            $childrenarray = array();
            $dataList = array();
            for ($index = 0; $index < count($openIDList); $index++) {
                $openid = $openIDList[$index];
                $userinfo = $wechatHelper->get_user_info($openid);
                $groupID = $wechatHelper->get_user_group($openid);

                $model = M('followerusergroup');
                $group_condition['PublicCodeID'] = 1;
                $group_condition['WeChatGroupID'] = $groupID["groupid"];
                $userList = $model->where($group_condition)->find();

                $groupName = $userList["GroupName"];

                $openIDArray = array("OpenID" => $openid, "NickName" => $userinfo["nickname"], "WechatGroupID" => $groupID["groupid"]
                    , "Subscribe" => $userinfo["subscribe"]
                    , "Subscribe_time" => $userinfo["subscribe_time"]
                    , "PublicCodeID" => 1);
                $children = array("userName" => $userinfo["nickname"], "groupName" => $groupName, "userId" => $openid, "groupId" => $groupID["groupid"]);
                array_push($dataList, $openIDArray);
                array_push($childrenarray, $children);
            }
            $followUser->addAll($dataList);
        } else {
            $childrenarray = array();
            for ($i = 0; $i < count($userList); $i++) {
                $model = M('followerusergroup');
                $group_condition['PublicCodeID'] = 1;
                $group_condition['WeChatGroupID'] = $userList[$i]["WechatGroupID"];
                $group = $model->where($group_condition)->find();
                $groupName = $group["GroupName"];
                $children = array("userName" => $userList[$i]["NickName"], "groupName" => $groupName, "userId" => $userList[$i]["OpenID"], "groupId" => $userList[$i]["WechatGroupID"]);
                array_push($childrenarray, $children);
            }
        }
        $viewarray = array("total" => $total, "rows" => $childrenarray);
        $json = json_encode($viewarray, JSON_UNESCAPED_UNICODE);
        $this->ajaxReturn($json, 'EVAL');
    }
    //编辑组名
    public function EditGroupName() {
        $groupID = I('groupId', '', "htmlspecialchars");
        $groupName = I('groupName', '', 'htmlspecialchars');
        $model = M('followerusergroup');
        $group_condition['PublicCodeID'] = 1;
        $result = FALSE;
        if ($groupID != '') {
            $group_condition['WeChatGroupID'] = $groupID;
            $result = $model->where($group_condition)->setField('GroupName', $groupName);
            if ($result == FALSE) {
                $this->ajaxReturn('n', 'EVAL');
            } else {
                $wechatHelper = new WechatHelperController();
                $result = $wechatHelper->update_groupName($groupid, $groupName);
                if ($result["errcode"] == 0) {
                    $this->ajaxReturn('y', 'EVAL');
                } else {
                    $this->ajaxReturn('n', 'EVAL');
                }
            }
        }
        else{
            $wechatHelper = new WechatHelperController();
            $result = $wechatHelper->create_group($groupName);
            
            if (!in_array("errcode", $result)) {
                $data["WeChatGroupID"] = $result["group"]["id"];
                $data["GroupName"] = $groupName;
                $data["PublicCodeID"]=1;
                $model->add($data);
            }
        }
    }
    //修改用户分组
    public function MoveUserToGroup() {
        $userID = I('userId', '', "htmlspecialchars");
        $groupID = I('groupId', '', 'htmlspecialchars');
        $model = M('followeruser');
        $user_condition['PublicCodeID'] = 1;
        $user_condition['OpenID'] = $userID;
        $result = $model->where($user_condition)->setField('WechatGroupID', $groupID);
        if ($result == FALSE) {
            $this->ajaxReturn('n', 'EVAL');
        } else {
            $wechatHelper = new WechatHelperController();
            $result = $wechatHelper->update_group($userID, $groupID);
            if ($result["errcode"] == 0) {
                $this->ajaxReturn('y', 'EVAL');
            } else {
                $this->ajaxReturn('n', 'EVAL');
            }
        }
    }

}
